/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package be.wiim;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

/**
 *
 * @author wim
 */
public class HudsonEnhancer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       try {
            String vHudsonURL = args[0];
            String vJob = args[1];
            HudsonController vHudsonController = new HudsonController(vHudsonURL,vJob);
            HudsonJob vLatest = vHudsonController.getfHudsonLatestJob();
              
            final GpioController vGpioController = GpioFactory.getInstance();
            final GpioPinDigitalInput vArcadeButton = vGpioController.provisionDigitalInputPin(RaspiPin.GPIO_04,PinPullResistance.PULL_DOWN);
            
            LedController vLedController = new LedController(vGpioController);
            vLedController.setLedsForHudsonStatus(vLatest.getfStatus());
            
            vArcadeButton.addListener(new GpioPinListenerDigital() {
                @Override
                public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent pEvent) {
                    System.out.println("GPIO PIN STATE CHANGE: " + pEvent.getPin() + " = " + pEvent.getState());
                    if(pEvent.getState().isHigh())
                    {
                        HudsonController.getInstance().startJob();
                        HudsonJob vHudsonJob = HudsonController.getInstance().latestStatusOfJob();
                        System.out.println("First check: "+vHudsonJob.getfJobName() + " - " + vHudsonJob.getfStatus());
                        while(vHudsonJob.getfStatus().indexOf(HudsonStatus.anime.toString()) > 0)
                        {
                            try {
                                    LedController.getInstance().setLedsForTempHudsonStatus(HudsonController.getInstance().getfHudsonLatestJob().getfStatus());
                                    Thread.sleep(1000);
                                    vHudsonJob = HudsonController.getInstance().latestStatusOfJob();
                                    System.out.println("Middle check: "+vHudsonJob.getfJobName() + " - " + vHudsonJob.getfStatus());

                                } 
                                catch (InterruptedException ex) 
                                {
                                    ex.printStackTrace();
                                }
                        }
                        System.out.println("After check: "+vHudsonJob.getfJobName() + " - " + vHudsonJob.getfStatus());
                        LedController.getInstance().setLedsForHudsonStatus(vHudsonJob.getfStatus());
                    }
                }

            });

            for (;;) {
                System.out.println("Going to sleep for 60 seconds.");
                Thread.sleep(60000);
            }

            } 
            catch (Exception ex) 
            {
                System.out.println("ERROR: <hudson-url> and/or <job-name> missing!");
            }
            
    }
    
}
